/**\file
 *
 * \copyright
 * Copyright (c) 2013, bourne++ Project Members
 * \copyright
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * \copyright
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * \copyright
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * \see Project Documentation: http://ef.gy/documentation/bourne++
 * \see Project Source Code: http://git.becquerel.org/jyujin/bourne++.git
 */

#if !defined(BOURNE_PROCESS_H)
#define BOURNE_PROCESS_H

#include <bourne++/expression.h>
#include <string>
#include <vector>
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <functional>
#include <algorithm>

namespace bourne
{
    class processStatus
    {
        public:
            constexpr processStatus (int pReturnCode)
                : returnCode(pReturnCode) {}

        protected:
            int returnCode;
    };

    class command
    {
        public:
            typedef processStatus evaluationType;

            command (const std::string &pName)
                : commandLine()
                {
                    commandLine.push_back([pName] () -> std::string { return pName; });
                }

            command &operator () (const std::string &pParameter)
            {
                commandLine.push_back([pParameter] () -> std::string { return pParameter; });
                return *this;
            }

            evaluationType evaluate (void) const
            {
                std::vector<std::string> cmds(commandLine.size());
                std::transform
                    (commandLine.begin(), commandLine.end(),
                     cmds.begin(), [&] (const std::function<std::string()> &f) -> std::string
                    {
                        return f();
                    });

                std::vector<const char*> cmd(commandLine.size());
                std::transform
                    (cmds.begin(), cmds.end(),
                     cmd.begin(), [&] (const std::string &s) -> const char *
                    {
                        return s.c_str();
                    });


                pid_t pid = fork();
                int status = 0;
                switch (pid)
                {
                    case -1:
                        status = -1;
                        break;
                    case 0:
                    {
                        execvp(cmd[0], (char*const*)cmd.data());
                        std::exit(-1);
                        break;
                    }
                    default:
                        do
                        {
                            waitpid(pid, &status, 0);
                        }
                        while (!WIFEXITED(status));
                        status = WEXITSTATUS(status);
                }
                std::cerr << status;
                return status;
            }

            std::vector<std::function<std::string()>> commandLine;
    };
};

#endif
